package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description NursingPlanServiceImpl
 * @Author lifuyun
 * @Date 2024-08-27
 */
@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlanVo> getAllNursingPlan() {
        List<NursingPlanVo> list = nursingPlanMapper.selectAllNursingPlan();
        return list;
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setCreateTime(LocalDateTime.now());
        nursingPlan.setStatus(1);
        nursingPlanMapper.insertNursingPlan(nursingPlan);

        Long planId = nursingPlan.getId();
        List<NursingProjectPlanVo> projectPlans = nursingPlan.getProjectPlans();

        projectPlans.forEach(projectPlan -> projectPlan.setPlanId(planId));
        projectPlans.forEach(projectPlan -> projectPlan.setCreateTime(LocalDateTime.now()));

        nursingProjectPlanMapper.insertNursingProjectPlan(projectPlans);
    }

    /**
     * 条件分页查询护理计划
     *
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> getByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> page = nursingPlanMapper.selectByPage(name, status);
        PageResponse<NursingPlanVo> objectPageResponse = new PageResponse<>(page);
        objectPageResponse.setRecords(page.getResult());
        return objectPageResponse;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);
        List<NursingProjectPlanVo> projectPlanVoList = nursingProjectPlanMapper.selectById(id);
        nursingPlan.setProjectPlans(projectPlanVoList);
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        return nursingPlanVo;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.updateById(nursingPlan);
        List<NursingProjectPlanVo> projectPlans = nursingPlan.getProjectPlans();
        nursingProjectPlanMapper.delectById(nursingPlan.getId());
        for (NursingProjectPlanVo projectPlan : projectPlans) {
            projectPlan.setPlanId(nursingPlan.getId());
        }
        nursingProjectPlanMapper.insertNursingProjectPlan(projectPlans);
    }

    /**
     * 删除护理项目
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        nursingProjectPlanMapper.delectById(id);
        nursingPlanMapper.delectById(id);
    }

    /**
     * 护理项目启用禁用
     *
     * @param id
     * @param status
     */
    @Override
    public void updateNursingPlanStatus(long id, int status) {
        nursingPlanMapper.updateNursingPlanStatus(id,status);
    }
}
